找传奇、传世资源到传世资源站!

android “摇一摇”动态更换皮肤 实例源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android “摇一摇”动态更换皮肤 实例源码下载 Android平台开发-第1张package com.shf.backgroundchange;import android.app.Activity;import android.app.Service;import android.content.Context;import android.content.res.Resources;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.Vibrator;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class TestActivity extends Activity {// 定义sensor管理器private SensorManager mSensorManager;// 震动Vibrator vibrator;// 震动监听器private sensorListener sensor;public static int flag = 0;private static LinearLayout L = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);L = (LinearLayout)findViewById(R.id.L);Resources r = getResources();L.setBackgroundDrawable(r.getDrawable(R.drawable.bg01));// 获取传感器管理服务mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);// 震动vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);// 监听器sensor = new sensorListener();// 加速度传感器mSensorManager.registerListener(sensor,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),// 还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等,// 根据不同应用,需要的反应速率不同,具体根据实际情况设定SensorManager.SENSOR_DELAY_NORMAL);}@Overrideprotected void onPause() {//mSensorManager.unregisterListener(sensor);super.onPause();}@Overrideprotected void onStop() {//mSensorManager.unregisterListener(sensor);super.onStop();} @Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();mHandler.removeMessages(0);}public Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);Resources r = getResources();switch (flag) {case 0:L.setBackgroundDrawable(r.getDrawable(R.drawable.bg01));break;case 1:L.setBackgroundDrawable(r.getDrawable(R.drawable.bg02));break;case 2:L.setBackgroundDrawable(r.getDrawable(R.drawable.bg03));break;case 3:L.setBackgroundDrawable(r.getDrawable(R.drawable.bg04));break;}} };private class sensorListener implements SensorEventListener {@Overridepublic void onAccuracyChanged(Sensor arg0, int arg1) {// TODO Auto-generated method stub}@Overridepublic void onSensorChanged(SensorEvent event) {int sensorType = event.sensor.getType();// values[0]:X轴,values[1]:Y轴,values[2]:Z轴float[] values = event.values;if (sensorType == Sensor.TYPE_ACCELEROMETER) {if ((Math.abs(values[0]) > 12 || Math.abs(values[1]) > 12 || Math.abs(values[2]) > 12)) {flag ;if(flag>3) flag = 0; long[] pattern = {500,500}; // OFF/ON/OFF/ON vibrator.vibrate(pattern,-1);mHandler.sendEmptyMessage(0);}}}}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复